﻿// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information.
// Website: https://admin.blazor.zone

using BlazorAdmin.Models;
using BlazorAdmin.Interface;
using BootstrapBlazor.Components;
using BlazorAdmin.Web.Shared.Extensions;
using Microsoft.AspNetCore.Components;
using System.Diagnostics.CodeAnalysis;

namespace BlazorAdmin.Web.Shared.Pages.Admin
{
    /// <summary>
    /// 
    /// </summary>
    public partial class Groups
    {
        [Inject]
        [NotNull]
        private DialogService? DialogService { get; set; }

        [Inject]
        [NotNull]
        private ToastService? ToastService { get; set; }

        [Inject]
        [NotNull]
        private IUser? UserService { get; set; }

        [Inject]
        [NotNull]
        private IRole? RoleService { get; set; }

        private async Task OnAssignmentUsers(SysGroup group)
        {
            List<SelectedItem> users = UserService.GetAll().ToSelectedItemList();
            List<int> values = UserService.GetUsersByGroupId(group.ID);

            await DialogService.ShowAssignmentDialog($"分配用户 - 当前部门: {group}", users, values, () =>
            {
                IEnumerable<int> ids = from u in users where u.Active select int.Parse(u.Value);
                bool result = UserService.SaveUsersByGroupId(group.ID, ids);
                return Task.FromResult(result);
            }, ToastService);
        }

        private async Task OnAssignmentRoles(SysGroup group)
        {
            List<SelectedItem> users = RoleService.GetAll().ToSelectedItemList();
            List<int> values = RoleService.GetRolesByGroupId(group.ID);

            await DialogService.ShowAssignmentDialog($"分配角色 - 当前部门: {group}", users, values, () =>
            {
                IEnumerable<int> ids = from u in users where u.Active select int.Parse(u.Value);
                bool result = RoleService.SaveRolesByGroupId(group.ID, ids);
                return Task.FromResult(result);
            }, ToastService);
        }
    }
}